/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.rcs.client;

import java.util.ServiceLoader;
import javax.servlet.http.HttpServletRequest;

/**
 *
 * @author pablo
 */
public class ClientFactory {
  
  private ServiceLoader<ClientCreator> clientCreators = ServiceLoader.load(ClientCreator.class);
  
  private static ClientFactory instance = new ClientFactory() ;
  
  private ClientFactory() {
    
  }
  
  public static ClientFactory get() {
    return instance ;
  }
  
  public ClientRender createClientRender(HttpServletRequest request) {
    ClientCreator creator = null ;
    ClientRender result = null ;
    int creatorValue = Integer.MIN_VALUE ;
    
    for (ClientCreator newCreator : clientCreators) {
      int value = newCreator.match(request);
      
      if (value > creatorValue) {
        creator = newCreator ;
        creatorValue = value ;
      }
    }
    
    if (null != creator)
      result = creator.create(request) ;
    
    return result ;
  }
}